/**
 * Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.restaurant.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.modules.restaurant.entity.UserSalary;
import org.springblade.modules.restaurant.vo.UserSalaryVO;
import org.springblade.modules.restaurant.mapper.UserSalaryMapper;
import org.springblade.modules.restaurant.service.IUserSalaryService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.math.BigDecimal;

/**
 * 用户薪资配置表 服务实现类
 *
 * @author Blade
 * @since 2025-02-18
 */
@Service
@AllArgsConstructor
public class UserSalaryServiceImpl extends BaseServiceImpl<UserSalaryMapper, UserSalary> implements IUserSalaryService {
	private IUserService userService;

	@Override
	public IPage<UserSalaryVO> selectUserSalaryPage(IPage<UserSalaryVO> page, UserSalaryVO userSalary) {
		return page.setRecords(baseMapper.selectUserSalaryPage(page, userSalary));
	}

	@Override
	public void updateRestDays(Long userId, Integer type) {
		BigDecimal restDays = type == 7 ? BigDecimal.valueOf(0.5) : BigDecimal.valueOf(1);
		UserSalary userSalary = getOne(Wrappers.<UserSalary>lambdaQuery().eq(UserSalary::getUserId, userId));
		userSalary.setRestDays(userSalary.getRestDays().subtract(restDays));
		updateById(userSalary);
	}

	@Override
	public boolean customizeSave(UserSalary userSalary) {
		Long userId = userSalary.getUserId();
		User user = userService.getById(userId);
		if (user != null) {
			userSalary.setUserId(user.getId());
			userSalary.setUserName(user.getRealName());
			userSalary.setTenantId(user.getTenantId());
			userSalary.setCreateDept(Long.valueOf(user.getDeptId()));
			userSalary.setCreateUser(user.getId());
		}
		userSalary.setUpdateUser(SecureUtil.getUserId());
		userSalary.setUpdateTime(DateUtil.date());

		int i;
		if (userSalary.getId() == null) {
			userSalary.setCreateTime(DateUtil.date());
			i = baseMapper.insert(userSalary);
		} else {
			i = baseMapper.updateById(userSalary);
		}
		return i > 0;
	}
}
